package com.naiterui.ehp.bs.doctor.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.domain.DrPatientRemarkname;
import com.naiterui.ehp.bs.doctor.dao.IDrPatientRemarkDao;
import com.naiterui.ehp.bs.doctor.service.IDrPatientRemarkService;

/**
 * @Description 患者备注信息Service实现类
 * @see IDrPatientRemarkService
 * @since v1.5
 */
@Service
public class AdDrPatientRemarkServiceImpl implements IDrPatientRemarkService {

    @Autowired
    private IDrPatientRemarkDao drPatientRemarkDao;

    /**
     * 根据医生ID和患者id查找相应的备注名列表
     */
    @Override
    public List<DrPatientRemarkname> getRemarkInfoByPatientIds(Long doctorId, List<Long> patientIds) {
        if (CollectionUtil.isEmpty(patientIds)) {
            return null;
        }
        return this.drPatientRemarkDao.getRemarkInfoList(doctorId, patientIds);
    }

    @Override
    public DrPatientRemarkname getRemarkInfo(Long doctorId, Long patientId) {
        return this.drPatientRemarkDao.getRemarkname(doctorId, patientId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateRemarkInfo(DrPatientRemarkname remarkInfo) {
        this.drPatientRemarkDao.saveOrUpdate(remarkInfo);
    }

    @Override
    public Map<Long, DrPatientRemarkname> getRemarkInfo2Map(Long doctorId) {
        return this.getRemarkInfo2Map(doctorId, Lists.newArrayList());
    }

    @Override
    public Map<Long, DrPatientRemarkname> getRemarkInfo2Map(Long doctorId, List<Long> patientIds) {
        List<DrPatientRemarkname> remarkInfoList = this.drPatientRemarkDao.getRemarkInfoList(doctorId, patientIds);

        Map<Long, DrPatientRemarkname> remarkInfoMap = Optional.ofNullable(remarkInfoList).orElse(Lists.newArrayList())
                                                               .parallelStream()
                                                               .collect(Collectors.toMap(DrPatientRemarkname::getPatientId, remarkInfo ->
                                                                       remarkInfo, (a, b) -> b));
        return remarkInfoMap;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDrPatientRemark(Long doctorId, Long patientId) {
        this.drPatientRemarkDao.remove(doctorId, patientId);
    }

}
